package com.dupf.product.web.controller.product;

import com.dupf.common.base.BaseController;
import com.dupf.common.util.ItvJsonUtil;
import com.dupf.product.common.constant.ProductResult;
import com.dupf.product.common.constant.ProductResultConstant;
import com.dupf.product.dao.model.ProductLogisticsSetting;
import com.dupf.product.dao.model.ProductLogisticsSettingExample;
import com.dupf.product.dao.model.ProductSalePriceSetting;
import com.dupf.product.dao.model.ProductSalePriceSettingExample;
import com.dupf.product.rpc.api.ProductLogisticsSettingService;
import com.dupf.product.rpc.api.ProductSalePriceSettingService;
import com.dupf.upms.common.constant.UpmsResult;
import com.dupf.upms.common.constant.UpmsResultConstant;
import com.google.common.base.Splitter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品controller
 * Created by dupf on 2018/3/20.
 */
@Controller
@Api(value = "派送方式", description = "派送方式设置")
@RequestMapping("/delivery")
public class ProductDeliveryMethodController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductDeliveryMethodController.class);

    @Autowired
    private ProductLogisticsSettingService productLogisticsSettingService;


    @ApiOperation(value = "物流设置列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                         @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                         HttpServletRequest request) throws IOException {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");

        ProductLogisticsSettingExample productLogisticsSettingExample = new ProductLogisticsSettingExample();
        productLogisticsSettingExample.createCriteria().andOrgIdEqualTo(orgId);

        List<ProductLogisticsSetting> rows = productLogisticsSettingService.selectByExampleForOffsetPage(productLogisticsSettingExample, offset, limit);
        long total = productLogisticsSettingService.countByExample(productLogisticsSettingExample);
        Map<String, Object> result = new HashMap<>();
        result.put("list", rows);
        result.put("total", total);
        return new ProductResult(ProductResultConstant.SUCCESS, result);
    }

    @ApiOperation(value = "添加物流设置")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HttpServletRequest request) throws IOException {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");

        String requestStr = super.getInputStream(request);
        JSONObject data = JSONObject.fromObject(requestStr);

        ProductLogisticsSetting productLogisticsSetting = ItvJsonUtil.readValue(data.toString(), ProductLogisticsSetting.class);
        productLogisticsSetting.setOrgId(orgId);
        productLogisticsSetting.setCtime(System.currentTimeMillis());
        productLogisticsSettingService.insertSelective(productLogisticsSetting);

        return new ProductResult(ProductResultConstant.SUCCESS, productLogisticsSetting.getId());
    }

    @ApiOperation(value = "修改物流设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws IOException {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");

        String requestStr = super.getInputStream(request);
        JSONObject data = JSONObject.fromObject(requestStr);

        ProductLogisticsSetting productLogisticsSetting = ItvJsonUtil.readValue(data.toString(), ProductLogisticsSetting.class);
        productLogisticsSetting.setOrgId(orgId);

        productLogisticsSettingService.updateByPrimaryKey(productLogisticsSetting);

        return new ProductResult(ProductResultConstant.SUCCESS, productLogisticsSetting.getId());
    }

    @ApiOperation(value = "删除物流设置")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids, HttpServletRequest request) throws IOException {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");

        if (StringUtils.isEmpty(ids)) {
            return new UpmsResult(UpmsResultConstant.SUCCESS, 0);
        }

        ProductLogisticsSettingExample productLogisticsSettingExample = new ProductLogisticsSettingExample();
        productLogisticsSettingExample.createCriteria().andOrgIdEqualTo(orgId).andIdIn(Splitter.on(',').trimResults().omitEmptyStrings()
                .splitToList(ids).stream().map(Integer::valueOf).collect(Collectors.toList()));

        int count = productLogisticsSettingService.deleteByExample(productLogisticsSettingExample);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }
}